package com.woogie.app;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.woogie.def.StartUpStatus;
import com.woogie.serv.async.DataToServer;
import com.woogie.serv.pojo.Register;


public class MainActivity extends ActionBarActivity {

    private static int state = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();

        ActionBar bar = getActionBar();
        bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        View view = View.inflate(getApplicationContext(), R.layout.top_bar_start,
                null);
        bar.setCustomView(view);
    }

    @Override
    public void onBackPressed() {
        View v = null;
        switch(state)
        {
            case 1:
            case 3:
                welcome();
                break;
            case 2:
                login(v);
                break;
            case 4:
            case 6:
                register(v);
                break;
            case 5:
                submitNewUser(v);
                break;
            case 0:
            default:
                finish();
        }
    }

    @Override
    public void finish() {
        new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Closing Setup")
                .setMessage("You are about to close the setup process.\nDo you want to continue?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }

                })
                .setNegativeButton("No", null)
                .show();
    }

    private void init(){
        state = StartUpStatus.NOT_REGISTERED;
        switch (state)
        {
            case StartUpStatus.NOT_REGISTERED:
                welcome();
                break;
            case StartUpStatus.REGISTERED:
                main();
                break;
            default:
                errorStartUp();
                break;
        }
    }

    private void welcome() {
        state = 0;
        setContentView(R.layout.welcome);
        TextView title = (TextView) findViewById(R.id.title);
        TextView title2 = (TextView) findViewById(R.id.title2);

        Typeface face = Typeface.createFromAsset(getAssets(),
                "fonts/Y2K_neophyte.ttf");
        title.setTypeface(face);
        title2.setTypeface(face);
    }

    private void main() {
        startActivity(new Intent(getBaseContext(), WoogieActivity.class));
    }

    private void errorStartUp() {
    }

    // Methods from xml buttons
    public void login(View v) {
        state = 1;
        setContentView(R.layout.login);
    }

    public void register(View v) {
        state = 3;
        setContentView(R.layout.new_user);
    }

    public void signIn(View v) {
        state = 2;
        //validations
        main();
    }

    public void forgotPw(View v) {
        state = 6;
        setContentView(R.layout.forgotpw);
    }

    public void forgotPwClick(View v) {
        login(v);
    }

    public void submitNewUser(View v) {
        state = 4;
        setContentView(R.layout.activate);
    }

    public void activateDevice(View v) {
        state = 5;
        //Validations
        main();
    }

    public void registerDevice()
    {
        new DataToServer(Register.getRegister(null, null, null, null).toString(), true).execute();
    }
}
